<template>
  <div class="dialog">
    <el-dialog width="45%" title="上传头像" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="上传头像：">
          <el-col :span="18">
            <UploadFile :file-list="form.headImg" :accept="'.jpg,.png'" @change="uploadHandle($event, 'headImg')">
              <div slot="tip" class="upload-tip">只能上传jpg/png文件，且不超过1MB</div>
            </UploadFile>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import UploadFile from '@/components/UploadFile/index.vue'

export default {
  name: 'Avatar',
  components: {
    UploadFile
  },

  data() {
    return {
      form: {
        headImg: ''
      },
      rules: {
        headImg: [{ required: true, message: '请上传头像', trigger: ['change'] }]
      }
    }
  },

  methods: {

    // 取消
    close() {
      this.$emit('close')
    },

    // 确认
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('confirm', this.form)
        } else {
          return false
        }
      })
    },

    // 上传图片处理
    uploadHandle(payload, name) {
      this.form[name] = payload.fileList[0]?.url
    }
  }

}
</script>

<style lang="scss" scoped></style>
